package org.summer.lawyer.controller.wx;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.idempotent.aspectj.RepeatSubmitAspect;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.satoken.utils.LoginHelper;
import org.summer.lawyer.domain.bo.article.ArticleBrowseBo;
import org.summer.lawyer.domain.bo.articlecomment.ArticleCommentBo;
import org.summer.lawyer.domain.bo.articleupvote.ArticleUpvoteBo;
import org.summer.lawyer.domain.vo.article.ArticleVo;
import org.summer.lawyer.domain.vo.articlecomment.ArticleCommentVo;
import org.summer.lawyer.domain.vo.articleupvote.ArticleUpvoteVo;
import org.summer.lawyer.service.IArticleCommentService;
import org.summer.lawyer.service.IArticleService;
import org.summer.lawyer.service.IArticleUpvoteService;

/**
 * 文章接口
 */

@Tag(name = "微信小程序文章相关接口", description = "微信小程序文章相关接口")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/wx/article")
public class WxArticleController {

    private final IArticleService articleService;

    private final IArticleCommentService articleCommentService;
    private final IArticleUpvoteService articleUpvoteService;
    private final RepeatSubmitAspect repeatSubmitAspect;


    @Parameters({
            @Parameter(name = "categoryId", description = "0或null最新，1每日普法，2司法判例，3古法研究院")
    })
    @SaIgnore
    @Operation(description = "根据文章类别查询文章列表")
    @GetMapping("/listByCategory")
    public TableDataInfo<ArticleVo> listByCategory(ArticleVo vo, PageQuery pageQuery) {
        vo.setStatus("1");
        pageQuery.setOrderByColumn("categorySort");
        TableDataInfo<ArticleVo> tableDataInfo = articleService.selectListPage(vo, pageQuery);
        if(CollUtil.isNotEmpty(tableDataInfo.getRows())){
            tableDataInfo.getRows().forEach(item ->{
                //查询点赞记录
                item.setUpvoteStatus(0);
                ArticleUpvoteVo upvoteVo = articleUpvoteService.selectVoByArticleIdAndWxUserId(item.getArticleId(),LoginHelper.getUserId());
                if(upvoteVo != null && upvoteVo.getCancelUpvote().equals("0")){
                    item.setUpvoteStatus(1);
                }
            });
        }
        return tableDataInfo;
    }


    @SaIgnore
    @Operation(description = "查询首页文章")
    @GetMapping("/listByHome")
    public TableDataInfo<ArticleVo> listByHome(ArticleVo vo) {
        vo.setCurated("1");
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageSize(3);
        TableDataInfo<ArticleVo> tableDataInfo = articleService.selectListPage(vo, pageQuery);
        if(CollUtil.isNotEmpty(tableDataInfo.getRows())){
            tableDataInfo.getRows().forEach(item ->{
                //查询点赞记录
                item.setUpvoteStatus(0);
                ArticleUpvoteVo upvoteVo = articleUpvoteService.selectVoByArticleIdAndWxUserId(item.getArticleId(),LoginHelper.getUserId());
                if(upvoteVo != null && upvoteVo.getCancelUpvote().equals("0")){
                    item.setUpvoteStatus(1);
                }
            });
        }
        return tableDataInfo;
    }

    @SaIgnore
    @GetMapping("/{id}")
    public R<ArticleVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        //增加浏览记录
        ArticleBrowseBo articleBrowseBo = new ArticleBrowseBo();
        articleBrowseBo.setArticleId(id);
        articleBrowseBo.setWxUserId(0L);
        if (StpUtil.isLogin()) {
            articleBrowseBo.setWxUserId(LoginHelper.getUserId());
        }
        articleService.saveArticleBrowse(articleBrowseBo);
        ArticleVo articleVo = articleService.selectArticleVoById(id);
        //查询点赞记录
        articleVo.setUpvoteStatus(0);
        ArticleUpvoteVo upvoteVo = articleUpvoteService.selectVoByArticleIdAndWxUserId(id,LoginHelper.getUserId());
        if(ObjectUtil.isNotNull(upvoteVo) && upvoteVo.getCancelUpvote().equals("0")){
            articleVo.setUpvoteStatus(1);
        }
        return R.ok();
    }

    @Parameters({
            @Parameter(name = "articleId", description = "文章ID", required = true),
            @Parameter(name = "wxUserId", description = "微信用户ID", required = true),
            @Parameter(name = "commentContent", description = "评论内容", required = true),
    })
    @Operation(summary = "保存文章评论信息", description = "保存文章评论信息")
    @SaCheckLogin
    @PostMapping("/comment")
    public R<ArticleVo> comment(@Validated @RequestBody ArticleCommentBo bo) {
        articleCommentService.insertByBo(bo);
        return R.ok("提交成功");
    }

    @Parameters(
            @Parameter(name = "articleId", description = "文章ID", required = true)
    )
    @Operation(summary = "查询文章评论列表", description = "查询文章评论列表")
    @SaIgnore
    @GetMapping("/listArticleComment")
    public TableDataInfo<ArticleCommentVo> listArticleComment(ArticleCommentBo bo, PageQuery pageQuery) {
        if (bo.getArticleId() == null) {
            bo.setArticleId(0L);
        }
        return articleCommentService.selectPageList(bo, pageQuery);
    }

    @Operation(summary = "文章点赞", description = "文章点赞")
    @Parameters({
            @Parameter(name = "articleId", description = "文章ID", required = true),
            @Parameter(name = "wxUserId", description = "微信用户ID", required = true)
    })
    @SaCheckLogin
    @PostMapping("/upvote")
    @RepeatSubmit
    public R<Void> upvote(@Validated @RequestBody ArticleUpvoteBo bo) {
        articleUpvoteService.insertByBo(bo);
        return R.ok();
    }

    @Operation(summary = "取消文章点赞", description = "取消文章点赞")
    @Parameters({
            @Parameter(name = "articleId", description = "文章ID", required = true),
            @Parameter(name = "wxUserId", description = "微信用户ID", required = true)
    })
    @SaCheckLogin
    @PostMapping("/cancelUpvote")
    @RepeatSubmit
    public R<Void> cancelUpvote(@Validated @RequestBody ArticleUpvoteBo bo) {
        articleUpvoteService.cancelUpvote(bo);
        return R.ok();
    }
}
